package com.locpx.lct;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class MainApp {
	static String lctImgFileName = "";
	static String bgImgFileName = "";
	
    public static void main(String[] args) {
    	
        final Properties props = new Properties();
        final File propFile = new File(".LCT_main");
        
        final Properties inputprops = new Properties();
        final File inputpropFile = new File(".input");
        
        if (propFile.exists()) {
            try {
                props.load(new FileInputStream(propFile));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                propFile.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        if (inputpropFile.exists()) {
            try {
                inputprops.load(new FileInputStream(inputpropFile));
                lctImgFileName = inputprops.getProperty("lct_image");
            	bgImgFileName = inputprops.getProperty("background_image");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
            	inputpropFile.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        final JFrame mainWindow = new JFrame();
        
        int width = Toolkit.getDefaultToolkit().getScreenSize().width/2;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height/2;
        
        mainWindow.setSize(width, height);
        
        mainWindow.setLocation(width/2, height/2);
        
        mainWindow.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent event) {
                try {
                    props.setProperty("language", Languages.VI.toString());
                    props.store(new FileOutputStream(propFile), null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }
            
        });
        
        final Properties messages = new Properties();
        try {
            messages.load(new InputStreamReader(new FileInputStream("messages_" + "VI" + ".txt"), "UTF-8"));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        
        JButton selectLCT = new JButton(messages.getProperty("button.select.lct"));
        JButton selectBG = new JButton(messages.getProperty("button.select.background"));
        
        
        JPanel buttonPane = new JPanel();
        buttonPane.add(selectLCT);
        buttonPane.add(selectBG);
        
        JPanel labelPane = new JPanel();
        final JLabel lctLbl = new JLabel(messages.getProperty("label.lct.location") + " " + messages.getProperty("default"));
        final JLabel bgLbl = new JLabel(messages.getProperty("label.bg.location") + " " + messages.getProperty("default"));
        
        if (lctImgFileName != null && !lctImgFileName.equals("")) {
        	lctLbl.setText(messages.getProperty("label.lct.location") + " " + lctImgFileName);
        }
        if (bgImgFileName != null && !bgImgFileName.equals("")) {
        	bgLbl.setText(messages.getProperty("label.bg.location") + " " + bgImgFileName);
        }
        
        labelPane.setLayout(new BoxLayout(labelPane, BoxLayout.PAGE_AXIS));
        labelPane.add(lctLbl);
        labelPane.add(bgLbl);
        
        selectLCT.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent event) {
            	int returnVal = LCTFileChooser.getInstance().showOpenDialog(mainWindow);
        		
        		if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                    	File file = LCTFileChooser.getInstance().getSelectedFile();
                        lctImgFileName = file.getAbsolutePath();
                        
                        lctLbl.setText(messages.getProperty("label.lct.location") + " " + lctImgFileName);
                        
                        inputprops.setProperty("lct_image", lctImgFileName);
						inputprops.store(new FileOutputStream(inputpropFile), null);
					} catch (Exception e) {
						e.printStackTrace();
					}
        		}
            }
        });
        
        selectBG.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent event) {
            	int returnVal = LCTFileChooser.getInstance().showOpenDialog(mainWindow);
        		
        		if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                    	File file = LCTFileChooser.getInstance().getSelectedFile();
                        bgImgFileName = file.getAbsolutePath();
                        
                        bgLbl.setText(messages.getProperty("label.bg.location") + " " + bgImgFileName);
                        
                        inputprops.setProperty("background_image", bgImgFileName);
						inputprops.store(new FileOutputStream(inputpropFile), null);
					} catch (Exception e) {
						e.printStackTrace();
					}
        		}
            }
        });
        
        
        JButton run = new JButton(messages.getProperty("button.start"));
        JPanel startPane = new JPanel();
        startPane.add(run);
        
        run.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent event) {
                try {
                    Runtime.getRuntime().exec("run_lct.bat");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        
        
        mainWindow.setLayout(new BorderLayout());
        mainWindow.getContentPane().add(buttonPane, BorderLayout.NORTH);
        mainWindow.getContentPane().add(labelPane, BorderLayout.CENTER);
        mainWindow.getContentPane().add(startPane, BorderLayout.SOUTH);
        
        
        mainWindow.setVisible(true);
    }
    
    
}
